//
//  FirstViewController.m
//  UI10_TableView__homework
//
//  Created by 洛洛大人 on 16/6/17.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "FirstViewController.h"
#import "SecondViewController.h"
@interface FirstViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, retain)NSArray *keysArray;

@property (nonatomic, retain)NSDictionary *dictionAry;


@end

@implementation FirstViewController
-(void)dealloc {
    [_keysArray release];
    [_dictionAry release];
    [super dealloc];
    
}



- (void)viewDidLoad {
    [super viewDidLoad];

    [self config];
    //处理数据:先
    [self handleData];
    //创建视图:后
    [self creatTableView];
    

    
}

-(void)config {
    
    //进行数据初始化
    self.dictionAry = [[NSDictionary alloc]init];
    
    self.keysArray = [[NSArray alloc]init];
    
    self.navigationItem.title = @"联系人";
    
    self.view.backgroundColor = [UIColor grayColor];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    
}


-(void)handleData {
    //获取文件路径
    NSString *path = [[NSBundle mainBundle] pathForResource:@"Contact" ofType:@".plist"];
    //在文件处获取数据
    self.dictionAry = [NSDictionary dictionaryWithContentsOfFile:path];
    
    self.keysArray = [self.dictionAry.allKeys sortedArrayUsingSelector:@selector(compare:)];
    
    
    }

//创建tableView
-(void)creatTableView {
    UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds) - 64) style:UITableViewStylePlain];
    
    [self.view addSubview:tableView];
    [tableView release];
    
    tableView.delegate = self;
    
    tableView.dataSource = self;
    
    tableView.rowHeight = 100;
    
    tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLineEtched;
    tableView.separatorColor = [UIColor yellowColor];
    
    [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    
    
}

//返回分区数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return self.dictionAry.count;
    
}


//返回每个分区的行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    NSString *key = [self.keysArray objectAtIndex:section];
    
    NSArray *array = [self.dictionAry objectForKey:key];
    
    return array.count;
    
}

//返回每行上的cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    NSArray *array = [self.dictionAry objectForKey:[self.keysArray objectAtIndex:indexPath.section]];
    
    NSDictionary *dic = [array objectAtIndex:indexPath.row];
    
    cell.textLabel.text = [dic objectForKey:@"name"];
    cell.textLabel.font = [UIFont systemFontOfSize:20.0];
    
    
    cell.imageView.backgroundColor = [UIColor cyanColor];
    
    NSString *imageName = [dic objectForKey:@"header"];
    
    UIImage *image = [UIImage imageNamed:imageName];
    

    
    cell.imageView.layer.cornerRadius = tableView.rowHeight / 2;
    
    cell.imageView.clipsToBounds = YES;
    
    
    
    [cell.imageView setImage:image];
    
    cell.accessoryType = UITableViewCellAccessoryDetailButton;
    
    
    
    return cell;
                    
}

//返回分区头得标题
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    
    NSString *headerString = [self.keysArray objectAtIndex:section];
    
    return headerString;
}

//侧边栏
-(NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{
    
    return self.keysArray;
}
//返回分布区头的高
-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForHeaderInSection:(NSInteger)section {
    
    return 25;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    SecondViewController *secondVC = [[SecondViewController alloc]init];
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    NSArray *array = [self.dictionAry objectForKey:[self.keysArray objectAtIndex:indexPath.section]];
    
    NSDictionary *dic = [array objectAtIndex:indexPath.row];
    
    secondVC.navigationItem.title = cell.textLabel.text;
    
    cell.textLabel.text = [dic objectForKey:@"name"];
    cell.textLabel.font = [UIFont systemFontOfSize:25.0];
    secondVC.navigationItem.title = cell.textLabel.text;
    UITextField *name = [[UITextField alloc]initWithFrame:CGRectMake(180, 280, 200, 50)];
    name.text = cell.textLabel.text;
    name.font = [UIFont systemFontOfSize:25.0];
    [secondVC.view addSubview:name];
    
    
    cell.textLabel.text = [dic objectForKey:@"QQ"];
    cell.textLabel.font = [UIFont systemFontOfSize:25.0];
    UITextField *QQ = [[UITextField alloc]initWithFrame:CGRectMake(180, 350, 200, 50)];
    QQ.text = cell.textLabel.text;
    QQ.font = [UIFont systemFontOfSize:25.0];
    [secondVC.view addSubview:QQ];
    
    cell.textLabel.text = [dic objectForKey:@"number"];
    cell.textLabel.font = [UIFont systemFontOfSize:25.0];
    UITextField *number = [[UITextField alloc]initWithFrame:CGRectMake(180, 420, 200, 50)];
    number.text = cell.textLabel.text;
    number.font = [UIFont systemFontOfSize:25.0];
    [secondVC.view addSubview:number];

    
    
    
    NSString *imageName = [dic objectForKey:@"header"];
    UIImage *image = [UIImage imageNamed:imageName];
    
    
    
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(70, 100, 150, 150)];
    
    imageView.layer.cornerRadius =imageView.frame.size.width / 2;
    
    cell.imageView.clipsToBounds = YES;
    
    [imageView setImage:image];
    
    [secondVC.view addSubview:imageView];
    
    
    [self.navigationController pushViewController:secondVC animated:YES];
    

    
    
    
}










@end
